package cn.bdqn.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.NewsCommentDao;
import cn.bdqn.pojo.News;
import cn.bdqn.pojo.NewsComment;

public class NewsCommentDaoImpl extends BaseDao implements NewsCommentDao {

	@Override
	public List<NewsComment> getNewsCommentListByNewsId(News news) {
		// TODO Auto-generated method stub
		List<NewsComment> newsCommentsList = new ArrayList<NewsComment>();
		if(this.getConnection()){
			String sql = "select * from news_comment where newsId=?";
			Object[] params = {news.getId()}; 
			try {
				ResultSet rs = this.executeQuery(sql, params);
				while(rs.next()){
					int id = rs.getInt("id");
					int nId = rs.getInt("newsId");
					String content = rs.getString("content");
					String author = rs.getString("author");
					String ip = rs.getString("ip");
					Timestamp createDate = rs.getTimestamp("createDate");
					
					NewsComment _newsComments = new NewsComment();
					_newsComments.setId(id);
					_newsComments.setNewsId(nId);
					_newsComments.setContent(content);
					_newsComments.setAuthor(author);
					_newsComments.setIp(ip);
					_newsComments.setCreateDate(createDate);
					newsCommentsList.add(_newsComments);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				this.closeResource();
			}
		}
		return newsCommentsList;
	}

	@Override
	public boolean add(NewsComment newsComment) {
		// TODO Auto-generated method stub
		boolean flag = false;
		if(this.getConnection()){
			String sql = "insert into news_comment (id,newsId,ip," +
					"content,author,createDate) values(?,?,?,?,?,?)";
			Object[] params = {newsComment.getId(),newsComment.getNewsId(),newsComment.getIp(),newsComment.getContent(),newsComment.getAuthor(),newsComment.getCreateDate()};
			try {
				int i = this.executeUpdate(sql, params);
				if(i > 0){
					System.out.println("add success!");
					flag = true;
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				this.closeResource();
			}
		}
		return flag;
	}

	@Override
	public boolean update(NewsComment newsComment) {
		// TODO Auto-generated method stub
		boolean flag = false;
		if(this.getConnection()){
			String sql = "update news_comment set content=? where id = ?";
			Object[] params = {newsComment.getContent(),newsComment.getId()};
			try {
				int i = this.executeUpdate(sql, params);
				if(i > 0){
					System.out.println("update success!");
					flag = true;
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				this.closeResource();
			}
		}
		return flag;
	}

	@Override
	public boolean deleteById(NewsComment newsComment) {
		// TODO Auto-generated method stub
		boolean flag = false;
		if(this.getConnection()){
			String sql = "delete from news_comment where id = ?";
			Object[] params = {newsComment.getId()};
			try {
				int i = this.executeUpdate(sql, params);
				if(i > 0){
					System.out.println("delelte by id success!");
					flag = true;
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				this.closeResource();
			}
		}
		return flag;
	}

	@Override
	public int deleteByNewsId(NewsComment newsComment) {
		// TODO Auto-generated method stub
		int flag = 0;
		if(this.getConnection()){
			String sql = "delete from news_comment where newsId=?";
			Object[] params = {newsComment.getNewsId()};
			try {
				flag = this.executeUpdate(sql, params);
				if(flag > 0){
					System.out.println("delete news Comment by newsId success!");
				}
			} catch (Exception e) {
				// TODO: handle exception
				flag = -1;
			}finally{
				this.closeResource();
			}
		}
		return flag;
	}

}
